home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pascalt1.zip / CHAP3.TXT < prev    next >
Text File  |  1988-01-15  |  18KB  |  455 lines

  1.  
  2.                  CHAPTER 3 - The simple Pascal data types
  3.  
  4.  
  5.              TURBO   Pascal  has  5  basic  data  types  which   are
  6.         predefined  and can be used anywhere in a  program  provided
  7.         you  use  them properly.  The five types and  a  very  brief
  8.         description follows;
  9.  
  10.             integer   Whole numbers from -32768 to 32767
  11.             byte      The integers from 0 to 255
  12.             real      Floating point numbers from 1E-38 to 1E+38
  13.             boolean   Can only have the value TRUE or FALSE
  14.             char      Any character in the ASCII character set
  15.  
  16.              Please note that the byte type of data is not a part of
  17.         the  standard  Pascal  definition  but  is  included  as  an
  18.         extension to the TURBO Pascal compiler.
  19.  
  20.              TURBO Pascal version 4.0 has three additional "integer"
  21.         types  available which are not available with  version  3.0,
  22.         and they are defined as follows;
  23.  
  24.             shortint  The integers from -128 to 127
  25.             word      The integers from 0 to 65535
  26.             longint   The integers from -2147483648 to 2147483647
  27.  
  28.              In  addition  to  the above  data  types  TURBO  Pascal
  29.         version  4.0 has the following data types available  but  in
  30.         order  to use them, you must have an 80X87 math  coprocessor
  31.         installed in your system;
  32.  
  33.             single    Real type with 7 significant digits
  34.             double    Real type with 15 significant digits
  35.             extended  Real type with 19 significant digits
  36.             comp      The integers from about -10E18 to 10E18
  37.  
  38.              A  complete definition of the available types for  each
  39.         compiler can be found on pages 41 and 42 of the TURBO Pascal
  40.         version 3.0 reference manual, and on pages 39 through 44  of
  41.         the  reference manual for version 4.0.  It would be good  to
  42.         read these pages now for a good definition prior to learning
  43.         how  to define and use them in a program.  Note that all  of
  44.         these will be used in example programs in this chapter.
  45.  
  46.              The integers are by far the easiest to understand so we
  47.         will start with a simple program that uses some integers  in
  48.         a  very simple way.  Load INTVAR into your TURBO system  and
  49.         lets take a look at it.
  50.  
  51.                             OUR FIRST VARIABLES
  52.  
  53.              Immediately following the program statement is  another
  54.         reserved word, "var".  This reserved word is used to  define
  55.         a  variable before it can be used anywhere in  the  program.
  56.  
  57.  
  58.                                   Page 13
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                  CHAPTER 3 - The simple Pascal data types
  69.  
  70.  
  71.         There is an unbroken rule of Pascal that states "Nothing can
  72.         be used until it is defined."  The compiler will complain by
  73.         indicating a compilation error if you try to use a  variable
  74.         without properly defining it.  It seems a bit bothersome  to
  75.         have  to  define every variable prior to its use,  but  this
  76.         rule  will  catch many spelling errors of  variables  before
  77.         they cause trouble.  Some other languages will simply define
  78.         a  new variable with the new name and go merrily on its  way
  79.         producing some well formatted garbage for you.
  80.  
  81.              Notice that there is only one "var", but it is used  to
  82.         define  three different variables, Count, X, and Y.  Once  a
  83.         var  is recognized, the compiler will continue to  recognize
  84.         variable definitions line after line until it finds  another
  85.         reserved word.  It would be permissible to put a var on  the
  86.         second line also but it is not necessary.  It would also  be
  87.         permissible to put all three variables on one line but  your
  88.         particular programming style will dictate where you put  the
  89.         three  variables.  Following the colon on each line  is  the
  90.         word  "integer"  which  is a standard  identifier  which  is
  91.         different from a reserved word.  An identifier is predefined
  92.         like a reserved word but you can redefine it thereby  losing
  93.         its  original purpose and meaning.  For now and for  a  long
  94.         time,  don't do that.  Page 38 contains a list  of  standard
  95.         identifiers in TURBO Pascal 3.0.  There is no  corresponding
  96.         list in the reference manual for TURBO Pascal 4.0.
  97.  
  98.                            OUR FIRST ARITHMETIC
  99.  
  100.              Now  that  we have three variables defined  as  integer
  101.         type variables, we are free to use them in a program in  any
  102.         way we desire as long as we use them properly.  If we  tried
  103.         to  assign a real value to X, the compiler will generate  an
  104.         error, once again preventing a garbage output.  Observe  the
  105.         start  of  the main body of the program.   There  are  three
  106.         statements  assigning  values to X, Y, and  Count.   A  fine
  107.         point of mathematics would state that Count is only equal to
  108.         the  value of X+Y until one of them was modified,  therefore
  109.         the  equal sign used in so many other languages is not  used
  110.         here.  The sign := is used, and can be read as "is  replaced
  111.         by  the  value of", when reading a listing to  preserve  the
  112.         mathematical  purity of Pascal.  Another quicker way  is  to
  113.         use the word "gets".  Thus X := X + 1 would be read "X  gets
  114.         the  value of X plus 1".  We will see later that the  simple
  115.         equal sign is reserved for use in a different manner.
  116.  
  117.              The  first three statements give X the value of  12,  Y
  118.         the  value  of 13, and Count the value of 12+13 or  25.   We
  119.         need  to  get those values out of the computer, so  we  need
  120.         another extension to the Writeln statement.  The first  part
  121.         of the data within the parentheses should be familiar to you
  122.  
  123.  
  124.                                   Page 14
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                  CHAPTER 3 - The simple Pascal data types
  135.  
  136.  
  137.         now,  but the second part is new.  Multiple outputs  can  be
  138.         handled within one Writeln if the fields are separated by  a
  139.         comma.   To output a variable, simply write  the  variable's
  140.         name in the output field.  The number following the variable
  141.         in  each case is the number of output columns to be used  by
  142.         the output data.  This number is optional and can be omitted
  143.         allowing the system to use as many columns as it needs.  For
  144.         purposes  of  illustration,  they  have  all  been  assigned
  145.         different  numbers  of  columns.  At  this  point,  you  can
  146.         compile and run INTVAR and examine its output.
  147.  
  148.              To  illustrate  the various ways to output  data,  load
  149.         INTVAR2   and  observe  that  even  though  the  output   is
  150.         identical,  it is output in a completely  different  manner.
  151.         Observe  especially  that  a Writeln all  by  itself  simply
  152.         moves the cursor to the beginning of a new line on the video
  153.         monitor.
  154.  
  155.              Compile and run this program and observe its output.
  156.  
  157.                       NOW LET'S USE LOTS OF VARIABLES
  158.  
  159.              Load  ALLVAR to observe a short program using all 5  of
  160.         the  basic  data types.  The variables are  simply  assigned
  161.         values and the values are printed.  A complete and  detailed
  162.         description of the options available in the Write  statement
  163.         is given in the TURBO reference manual version 3.0 on  pages
  164.         111  through 113, and on pages 500 through 502  for  version
  165.         4.0.  It would be to your advantage to read this section  at
  166.         this time since very little explanation will be given  about
  167.         Write  statements from this point on.  We will  discuss  the
  168.         method  by which we can write to disk files or other  output
  169.         devices when the time comes.
  170.  
  171.              Back  to  the basic types.  Pascal does lots  of  cross
  172.         checking  for obvious errors.  It is illegal to  assign  the
  173.         value of any variable with a value that is of the wrong type
  174.         or outside the allowable range of that variable.  There  are
  175.         routines to convert from one system to another when that  is
  176.         necessary.  Suppose, for example, that you wished to use the
  177.         value of an integer in a calculation of real numbers.   That
  178.         is  possible  by first converting the integer  into  a  real
  179.         number  of  the  same  value and using  the  new  real  type
  180.         variable  in  the desired calculations.  The new  real  type
  181.         variable  must of course be defined in a var statement as  a
  182.         real type variable before it can be used.  Details of how to
  183.         do the conversion will be given later.
  184.  
  185.              Since we have some variables defined, it would be  nice
  186.         to  use  the  properties of computers  for  which  they  are
  187.         famous, namely some mathematics.  Two programs are available
  188.  
  189.  
  190.                                   Page 15
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                  CHAPTER 3 - The simple Pascal data types
  201.  
  202.  
  203.         for your observation to illustrate the various kinds of math
  204.         available, REALMATH using real variables, and INTMATH  using
  205.         integer variables.  You can edit, compile, and run these  on
  206.         your  own  with  no  comment from  me  except  the  comments
  207.         embedded into the source files.  Chapter 6 on pages 51 to 54
  208.         of  your  version  3.0  TURBO  reference  manual  completely
  209.         defines the simple mathematics available.  The corresponding
  210.         list  for  version  4.0 is found in chapter 3  on  pages  46
  211.         through 49.
  212.  
  213.              A  byte  type  variable is used just  like  an  integer
  214.         variable  but with a much smaller value.  Only one  byte  of
  215.         computer memory is used for each variable defined as a  byte
  216.         type  variable,  but  2  are  used  for  each  integer  type
  217.         variable.
  218.  
  219.                              BOOLEAN VARIABLES
  220.  
  221.              Lets take a look at the boolean variable which is  only
  222.         allowed  to  take on two different values,  TRUE  or  FALSE.
  223.         This  variable  is  used  for loop  controls,  end  of  file
  224.         indicators  or  any other TRUE or FALSE  conditions  in  the
  225.         program.   Variables can be compared to determine a  boolean
  226.         value.  Following  is  a complete  list  of  the  relational
  227.         operators available with Pascal.
  228.  
  229.              =    equal to
  230.              <>   not equal to
  231.              >    greater than
  232.              <    less than
  233.              >=   greater than or equal to
  234.              <=   less than or equal to
  235.  
  236.              These  operators  can  be used to compare  any  of  the
  237.         simple types of data including integer, char, byte, and real
  238.         type  variables or constants.  An illustration is  the  best
  239.         way to learn about the boolean variable so load BOOLMATH and
  240.         observe it.
  241.  
  242.              In  BOOLMATH we define a few boolean variables and  two
  243.         integer  type variables for use in the program and begin  by
  244.         assigning   values  to  the  two  integer  variables.    The
  245.         expression  "Junk  = Who" in line 14 is actually  a  boolean
  246.         operation  that is not true since the value of Junk  is  not
  247.         equal  to the value of Who.  The result is  therefore  FALSE
  248.         and  that value is assigned to the boolean variable A.   The
  249.         boolean variable B is assigned the value of TRUE because the
  250.         expression  "Junk  =  (Who  -  1)"  is  true.   The  boolean
  251.         variables  C  and D are likewise assigned some values  in  a
  252.         manner that should not need any comment.  After assigning  a
  253.  
  254.  
  255.  
  256.                                   Page 16
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                  CHAPTER 3 - The simple Pascal data types
  267.  
  268.  
  269.         value to the variable with the big name, the values are  all
  270.         printed out.
  271.  
  272.                    WHERE DO WE USE THE BOOLEAN VARIABLES?
  273.  
  274.              We  will find many uses for the boolean  type  variable
  275.         when we study the loops and conditional statements soon, but
  276.         until  then  we can only learn what they are.  Often,  in  a
  277.         conditional  statement,  you will want to  do  something  if
  278.         either  of two things are true, in which case you  will  use
  279.         the  reserved word "and" with two boolean  expressions.   If
  280.         either  of the two are true, the result will be true.   Line
  281.         29  is an example of this.  If the boolean variables  B,  C,
  282.         and D, are all true, then the result will be true and A will
  283.         be assigned the value of TRUE.  If any one of them is false,
  284.         the result will be false and A will be assigned the value of
  285.         FALSE.
  286.  
  287.              In Line 31, where the "or" operator is illustrated,  if
  288.         any of the three boolean variables is true, the result  will
  289.         be  true,  and if all three are false, the  result  will  be
  290.         false.   Another  boolean  operator is the  "not"  which  is
  291.         illustrated  in  line 30.  Examine line 33  which  says  the
  292.         result  is true only if the variable Junk is one  less  than
  293.         Who, or if Junk is equal to Who.
  294.  
  295.              Compile and run this program, then add some  additional
  296.         printout to see if the boolean variables change the way  you
  297.         think they should in the last few statements.
  298.  
  299.                     LETS LOOK AT THE CHAR TYPE VARIABLE
  300.  
  301.              A  char  type variable is a very useful  variable,  but
  302.         usually not when used alone.  It is very powerful when  used
  303.         in an array or some other user defined data structure  which
  304.         is beyond the scope of this chapter.  A very simple program,
  305.         CHARDEMO is included to give you an idea of how a char  type
  306.         variable  can be used.  Study then compile and run  CHARDEMO
  307.         for a very brief idea of what the char type variable is used
  308.         for.
  309.  
  310.              Examine the sample program CONVERT for several examples
  311.         of converting data from one simple variable to another.  The
  312.         program is self explanatory.
  313.  
  314.                      THIS IS FOR TURBO PASCAL 4.0 USERS
  315.  
  316.              If  you  are using TURBO Pascal version  3.0,  you  are
  317.         finished   with   this  chapter  because  the   data   types
  318.         illustrated in the last two programs are not available  with
  319.         that compiler.
  320.  
  321.  
  322.                                   Page 17
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                  CHAPTER 3 - The simple Pascal data types
  333.  
  334.  
  335.  
  336.              If you are using TURBO Pascal 4.0, display the  program
  337.         NEWINT4  for an example of using the extended integer  types
  338.         available  with that compiler.  Four variables  are  defined
  339.         and values assigned to each, then the results are displayed.
  340.         When you compile and run the program, you will see that  the
  341.         variable Big_int can indeed handle a rather large number.
  342.  
  343.              It must be pointed out that the calculation in lines 13
  344.         and 21 result in a different answer even though they  appear
  345.         to  be  calculating the same thing.  An  explanation  is  in
  346.         order.  The quantity named MaxInt used in lines 10 and 13 is
  347.         a constant built into the system that represents the largest
  348.         value that an integer type variable can store.  On the first
  349.         page  of  this  chapter we defined that as  32767  and  when
  350.         running  the program you will find that Index displays  that
  351.         value as it should.  The constant MaxInt has a type that  is
  352.         of  a  universal_integer  type  as do  all  of  the  numeric
  353.         constants in line 13.  The result then is calculated to  the
  354.         number of significant digits dictated by the left hand  side
  355.         of  the  assignment  statement  which  is  of  type  longint
  356.         resulting in a very large number.
  357.  
  358.              When we get to line 21, however, the variable Index  is
  359.         of  type integer so the calculations are done as though  the
  360.         constants were of type integer also which causes some of the
  361.         more  significant  digits to be  truncated.   The  truncated
  362.         result  is  converted to type longint and  assigned  to  the
  363.         variable  Big_int  and the truncated value is  displayed  by
  364.         line 22.
  365.  
  366.              After that discussion it should be apparent to you that
  367.         it  is important what types you use for your variables.   It
  368.         must  be  emphasized that it would not be wise  to  use  all
  369.         large type variables because they use more storage space and
  370.         slow down calculations.  Experience will dictate the  proper
  371.         data types to use for each application.
  372.  
  373.                          NOW FOR THE NEW REAL TYPES
  374.  
  375.              If you are using TURBO Pascal 4.0, display the  program
  376.         NEWREAL4 for an example using the new "real" types available
  377.         with  version  4.0.  Note that you must have an  80X87  math
  378.         coprocessor  installed  to  compile and  run  this  program.
  379.         There is a note given in the file to aid you in selecting it
  380.         for use.
  381.  
  382.              This program should be self explanatory so nothing will
  383.         be  said  except that when you run it you  can  observe  the
  384.         relative  accuracy  of  each of the  variable  types.   Once
  385.  
  386.  
  387.  
  388.                                   Page 18
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                  CHAPTER 3 - The simple Pascal data types
  399.  
  400.  
  401.         again, you should keep in mind that use of the larger "real"
  402.         types costs you a bit in storage space and run-time speed.
  403.  
  404.  
  405.                            PROGRAMMING EXERCISE
  406.  
  407.         1.  Write a program containing several variable  definitions
  408.             and do some math on them, printing out the results.
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 19
  455.